package com.whfc.fuum.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.fuum.dao.AppCorpMapper;
import com.whfc.fuum.dao.SysDeptMapper;
import com.whfc.fuum.dao.SysDictMapper;
import com.whfc.fuum.dto.AppCorpDTO;
import com.whfc.fuum.entity.AppCorp;
import com.whfc.fuum.param.AppCorpAddParam;
import com.whfc.fuum.param.AppCorpEditParam;
import com.whfc.fuum.param.AppCorpListQO;
import com.whfc.fuum.service.AppCorpService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 合作单位
 *
 * @author likang
 * @Description:
 * @date 2019年11月18日
 */
@DubboService(interfaceClass = AppCorpService.class, version = "1.0.0")
public class AppCorpServiceImpl implements AppCorpService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppCorpMapper appCorpMapper;

    @Autowired
    private SysDictMapper sysDictMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;


    @Override
    public List<AppCorpDTO> list(Integer deptId) {
        return appCorpMapper.selectByDeptId(deptId);
    }

    @Override
    public List<AppCorpDTO> list(List<Integer> corpIds) throws BizException {
        logger.info("合作单位名称列表，corpIds:{}", corpIds);
        return appCorpMapper.selectByCorpIds(corpIds);
    }

    @Override
    public PageData<AppCorpDTO> list(AppCorpListQO request) {

        Integer pageNum = request.getPageNum();
        Integer pageSize = request.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<AppCorpDTO> list = appCorpMapper.selectByParam(request);
        PageHelper.clearPage();

        for (AppCorpDTO appCorpDTO : list) {
            String deptName = sysDeptMapper.selectDeptNameById(appCorpDTO.getDeptId());
            String corpTypeName = sysDictMapper.selectNameById(appCorpDTO.getCorpTypeId());
            appCorpDTO.setDeptName(deptName);
            appCorpDTO.setCorpTypeName(corpTypeName);
        }
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public AppCorpDTO detail(Integer corpId) {
        logger.info("查询合作单位详情，corpId:{}", corpId);
        AppCorpDTO appCorpDTO = appCorpMapper.selectDetailByCorpId(corpId);
        if (appCorpDTO != null) {
            String corpTypeName = sysDictMapper.selectNameById(appCorpDTO.getCorpTypeId());
            appCorpDTO.setCorpTypeName(corpTypeName);
        }
        return appCorpDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AppCorpAddParam request) {

        String corpCode = request.getCorpCode();
        AppCorp appCorp = appCorpMapper.selectByDeptIdAndCorpCode(request.getDeptId(), corpCode);
        if (appCorp != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该合作单位已存在不能重复添加!");
        }

        //向app_corp中插入数据
        AppCorp record = new AppCorp();
        BeanUtils.copyProperties(request, record);
        record.setCorpType(request.getCorpTypeId());
        appCorpMapper.insertSelective(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(AppCorpEditParam request) {
        Integer corpId = request.getCorpId();
        String corpCode = request.getCorpCode();
        AppCorp appCorp = appCorpMapper.selectByDeptIdAndCorpCode(request.getDeptId(), corpCode);
        if (appCorp != null && !corpId.equals(appCorp.getId())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该合作单位已存在!");
        }

        AppCorp record = new AppCorp();
        BeanUtils.copyProperties(request, record);
        record.setCorpType(request.getCorpTypeId());
        record.setId(corpId);
        appCorpMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Integer corpId) {
        if (corpId == null) {
            return;
        }
        appCorpMapper.deleteLogicByCorpId(corpId);
    }

    @Override
    public AppCorpDTO getCorpByDeptIdAndName(Integer deptId, String name) throws BizException {
        AppCorp appCorp = appCorpMapper.selectByDeptIdAndCorpName(deptId, name);
        if (appCorp == null) {
            return null;
        }
        AppCorpDTO appCorpDTO = new AppCorpDTO();
        BeanUtils.copyProperties(appCorp, appCorpDTO);
        appCorpDTO.setCorpId(appCorp.getId());
        return appCorpDTO;
    }
}